<?php

namespace app\controller;

use think\facade\Db;
use think\response\Json;


class GameVideo
{

    //todo 图片要实时上传,图片上传到/game/game/里             视频要异步上传
    public function uploadFile($url,$path):  string
    {
        try {
            $cosUploader = new CosUploader(config('basic.cos.bucket'), config('basic.cos.region'), config('basic.cos.secretId'), config('basic.cos.secretKey'));
            $res = $cosUploader->uploadFileFromUrl($url, $path);
            return '/'.$res['Key'];

        } catch (\Exception $e) {
            return "";
        }
    }

    //处理这个表里面的game_video的链接, 如果有http://video的数据, 找出来, 然后存进cos, 再将得到的路径 存入数据库
    public function videoToCos(): Json
    {
        try {
            $game_product = Db::name("game_product")
                ->where("game_video", "like", "%http://video%")
                ->field('id,game_video,created_at')
                ->limit(10)
                ->order("created_at", "desc")
                ->select();
            foreach ($game_product as $value) {
                $game_video = $value['game_video'];
                $res = $this->uploadFile($game_video, "/game/video/");
                if ($res) {
                    Db::name("game_product")->where("id", $value['id'])->update(['game_video' => $res]);
                }
            }
            return json(['code' => 0, 'msg' => '处理视频上传到Cos完成']);
        } catch (\Exception $e) {
            var_dump($e->getMessage());
            return json(['code' => -1, 'msg' => '处理视频上传到Cos失败']);
        }
    }


}    